//package cn.nfwx.tv.controller;
//
//import android.app.Activity;
//import android.app.Dialog;
//import android.os.Handler;
//import android.os.Message;
//import android.text.InputFilter;
//import android.text.TextUtils;
//import android.text.method.HideReturnsTransformationMethod;
//import android.text.method.PasswordTransformationMethod;
//import android.util.Log;
//import android.view.Display;
//import android.view.View;
//import android.view.Window;
//import android.view.WindowManager;
//import android.webkit.WebView;
//import android.webkit.WebViewClient;
//import android.widget.Button;
//import android.widget.CheckBox;
//import android.widget.CompoundButton;
//import android.widget.EditText;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.TypeReference;
//import com.google.gson.Gson;
//import com.kaopiz.kprogresshud.KProgressHUD;
//import com.tencent.mm.opensdk.modelmsg.SendAuth;
//import com.tencent.mm.opensdk.openapi.IWXAPI;
//import com.tencent.mm.opensdk.openapi.WXAPIFactory;
//
//import org.greenrobot.eventbus.EventBus;
//import org.json.JSONObject;
//
//import cn.nfwx.tv.R;
//import cn.nfwx.tv.base.NFWXApplication;
//import cn.nfwx.tv.callback.ResponseCallback;
//import cn.nfwx.tv.callback.ResponseListener;
//import cn.nfwx.tv.callback.ResponseWithFailListener;
//import cn.nfwx.tv.callback.UserCallBack;
//import cn.nfwx.tv.conts.Constants;
//import cn.nfwx.tv.events.LoginEvents;
//import cn.nfwx.tv.events.ToMineItemEvent;
//import cn.nfwx.tv.fragment.me.MineFragment;
//import cn.nfwx.tv.helper.AccountHelper;
//import cn.nfwx.tv.helper.DialogFragmentHelper;
//import cn.nfwx.tv.helper.EditNullFilter;
//import cn.nfwx.tv.helper.NetworkHelper;
//import cn.nfwx.tv.helper.ValueCallback;
//import cn.nfwx.tv.helper.WifiHelper;
//import cn.nfwx.tv.manager.UserManager;
//import cn.nfwx.tv.model.Result;
//import cn.nfwx.tv.model.User;
//import cn.nfwx.tv.model.UserModel;
//import cn.nfwx.tv.okhttp.UrlHelper;
//import cn.nfwx.tv.util.LogUtil;
//import cn.nfwx.tv.util.MD5Util;
//import cn.nfwx.tv.util.NetworkUtil;
//import cn.nfwx.tv.util.SharePrefUtil;
//import cn.nfwx.tv.util.ToastUtil;
//import cn.nfwx.tv.util.ToolUtil;
//import cn.nfwx.tv.vo.ResultErrorVo;
//
///**
// * Created by dell on 2017/11/20.
// */
//
//public class RegisterViewController implements View.OnClickListener, NetworkHelper.NetworkStateListener {
//    private final static String TAG_LOG = RegisterViewController.class.getSimpleName();
//    private Activity activity;
//    private CheckBox cb_register_appiont, register_cb;
//    private EditText ed_register_phone, ed_register_code, ed_register_name, ed_register_pw;
//    private Button register_btn;
//    private TextView btn_register_code, agreement_btn;
//    private String phoneNo;
//    private int countDown = 60;
//    private final int MSG_COUNT = 1;
//    private KProgressHUD registerProgress, smsProgress;
//    private LinearLayout login_wechat_lin;
//    private IWXAPI iwxapi;
//    private boolean canSendSms = false;
//
//    public RegisterViewController(Activity activity, View view) {
//        this.activity = activity;
//        initView(view);
//        NetworkHelper.getInstance().addListener(this);
//    }
//
//    public void initView(View view) {
//        agreement_btn = findView(view, R.id.agreement_btn);
//        cb_register_appiont = findView(view, R.id.cb_register_appiont);
//        register_cb = findView(view, R.id.register_cb);
//        ed_register_phone = findView(view, R.id.ed_register_phone);
//        ed_register_code = findView(view, R.id.ed_register_code);
//        ed_register_name = findView(view, R.id.ed_register_name);
//        ed_register_pw = findView(view, R.id.ed_register_pw);
//        register_btn = findView(view, R.id.register_btn);
//        btn_register_code = findView(view, R.id.btn_register_code);
//        login_wechat_lin = findView(view, R.id.login_wechat_lin);
//        register_btn.setOnClickListener(this);
//        btn_register_code.setOnClickListener(this);
//        agreement_btn.setOnClickListener(this);
//        login_wechat_lin.setOnClickListener(this);
//        ed_register_phone.setFilters(new InputFilter[]{new EditNullFilter()});
//        ed_register_code.setFilters(new InputFilter[]{new EditNullFilter()});
//        ed_register_name.setFilters(new InputFilter[]{new EditNullFilter()});
//        ed_register_pw.setFilters(new InputFilter[]{new EditNullFilter()});
//        register_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (b) {
//                    ed_register_pw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                } else {
//                    ed_register_pw.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                }
//            }
//        });
//        ToolUtil.toCheckPhone(activity, ed_register_phone, 2, new ValueCallback<Boolean>() {
//            @Override
//            public void onReceiveValue(Boolean value) {
//                canSendSms = value;
//            }
//        });
//        ToolUtil.toSetEditTextLength(activity, ed_register_code, 6);
//        ToolUtil.toSetEditTextLength(activity, ed_register_pw, 15);
//        iwxapi = NFWXApplication.getInstance().getIwxapi();
//        if (NetworkUtil.getNetworkType(activity) == NetworkUtil.WIFI_NETWORK) {
//            WifiHelper.getInstance()._testIfNeedLogon(new ValueCallback<Boolean>() {
//                @Override
//                public void onReceiveValue(Boolean value) {
//                    if (value) {
//                        shandler.sendEmptyMessage(0);
//                    } else {
//                        shandler.sendEmptyMessage(1);
//                    }
//                }
//            }, "wifi");
//        } else {
//            shandler.sendEmptyMessage(0);
//        }
//    }
//
//
//    /**
//     * 查找控件
//     *
//     * @param rid
//     * @param <T>
//     * @return
//     */
//    public <T extends View> T findView(View view, int rid) {
//        return (T) view.findViewById(rid);
//    }
//
//
//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.agreement_btn:
//                View dialogView = View.inflate(activity, R.layout.dialog_agreement, null);
//                WebView webView = dialogView.findViewById(R.id.webview_agreenment);
//                webView.loadUrl(UrlHelper.getAppAgreement());
//                webView.setWebViewClient(new WebViewClient() {
//                    @Override
//                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                        view.loadUrl(url);
//                        return super.shouldOverrideUrlLoading(view, url);
//                    }
//                });
//                Dialog dialog = getDialog(dialogView);
//                dialog.show();
//                break;
//            case R.id.register_btn:
//                phoneNo = ed_register_phone.getText().toString();
//                if (TextUtils.isEmpty(phoneNo)) {
//                    Toast.makeText(activity, R.string.toast_phone, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (!ToolUtil.isPhoneLegal(phoneNo)) {
//                    Toast.makeText(activity, R.string.toast_right_phone, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                String smsCode = ed_register_code.getText().toString();
//                if (TextUtils.isEmpty(smsCode)) {
//                    Toast.makeText(activity, R.string.toast_code, Toast.LENGTH_LONG).show();
//                    return;
//                }
//
//                if (smsCode.length() != 6 || !ToolUtil.isNumeric(smsCode)) {
//                    Toast.makeText(activity, R.string.toast_code_length, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                final String name = ed_register_name.getText().toString();
//                if (TextUtils.isEmpty(name)) {
//                    Toast.makeText(activity, R.string.toast_nickname, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (name.length() > 10) {
//                    Toast.makeText(activity, R.string.toast_nickname_length, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                final String password = ed_register_pw.getText().toString();
//                if (TextUtils.isEmpty(password)) {
//                    Toast.makeText(activity, R.string.toast_pw, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (password.length() > 15 || password.length() < 6) {
//                    Toast.makeText(activity, R.string.toast_pw_length, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (cb_register_appiont.isChecked()) {
//                    if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_P2P) {
//                        registerProgress = KProgressHUD.create(activity)
//                                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
////                        .setLabel("加载中")
////                        .setDetailsLabel("Downloading data")
//                                .setCancellable(true)
//                                .setAnimationSpeed(2)
//                                .setDimAmount(0.5f);
//                        registerProgress.show();
//                        UserManager.register(activity, "sms", phoneNo, name, smsCode, password, new UserCallBack() {
//                            @Override
//                            public void onSuccess(final UserModel userModel) {
//                                LogUtil.d(TAG_LOG, userModel.getUser().getAccountId());
//                                activity.runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        if (userModel != null) {
//                                            SharePrefUtil.saveUser(activity, userModel);
//                                            Toast.makeText(activity, "注册成功", Toast.LENGTH_LONG).show();
//                                            EventBus.getDefault().post(new LoginEvents(userModel));
//                                            registerProgress.dismiss();
//                                        }
//                                    }
//                                });
//
//                            }
//
//                            @Override
//                            public void onComplete() {
//                                super.onComplete();
//                                activity.runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        registerProgress.dismiss();
//                                    }
//                                });
//                            }
//
//                            @Override
//                            public void onFail(String code, final String response) {
//                                activity.runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        if (!TextUtils.isEmpty(response)) {
//                                            Gson gson = new Gson();
//                                            ResultErrorVo resultErrorVo = gson.fromJson(response, ResultErrorVo.class);
//                                            Toast.makeText(activity, resultErrorVo.getMsg() + "", Toast.LENGTH_LONG).show();
//                                        }
//                                        registerProgress.dismiss();
//                                    }
//                                });
//                            }
//                        });
//                    } else if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_QPOP) {
//                        if (canSendSms) {
//                            Toast.makeText(activity, "该号码已注册", Toast.LENGTH_LONG).show();
//                            return;
//                        }
//                        registerProgress = KProgressHUD.create(activity)
//                                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
//                                .setCancellable(true)
//                                .setAnimationSpeed(2)
//                                .setDimAmount(0.5f);
//                        registerProgress.show();
//                        UserManager.requestIjkRegister(activity, phoneNo, name, MD5Util.MD5(password), smsCode, new ResponseListener<JSONObject>() {
//                            @Override
//                            public void onResponse(String requestTag, final JSONObject
//                                    response) {
//                                activity.runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        registerProgress.dismiss();
//                                        final Result<User> result = JSON.parseObject(response.toString(), new TypeReference<Result<User>>() {
//                                        }.getType());
//                                        if (result.getMessage().equals(Result.RESULT_SUCCESS)) {
//                                            if (result.getData() != null && result.getData().getUid() != 0) {
//                                                AccountHelper.getInstance().saveUser(activity, result.getData());
//                                                ToastUtil.show(activity, "注册成功");
//
//                                                EventBus.getDefault().post(new ToMineItemEvent(MineFragment.POSITION_SUBSCRIPT));
//                                            }
//                                        } else {
//                                            ToastUtil.show(activity, result.getMessage());
//                                        }
//                                    }
//                                });
//                            }
//
//                            @Override
//                            public void onError(String requestTag, Throwable t) {
//                                activity.runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        registerProgress.dismiss();
//                                        ToastUtil.show(activity, "注册失败", Toast.LENGTH_LONG);
//                                    }
//                                });
//                            }
//
//                            @Override
//                            public void onFinish() {
//
//                            }
//                        });
//                    }
//                } else {
//                    Toast.makeText(activity, "请勾选注册协议", Toast.LENGTH_LONG).show();
//                }
//                break;
//            case R.id.btn_register_code:
//                phoneNo = ed_register_phone.getText().toString();
//                if (TextUtils.isEmpty(phoneNo)) {
//                    Toast.makeText(activity, R.string.toast_code, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (!ToolUtil.isPhoneLegal(phoneNo)) {
//                    Toast.makeText(activity, R.string.toast_right_phone, Toast.LENGTH_LONG).show();
//                    return;
//                }
//                btn_register_code.setClickable(false);//防止重复点击Button，否则多次点击button：每隔一秒发送消息多次执行
//                if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_P2P) {
//                    smsProgress = KProgressHUD.create(activity)
//                            .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
////                        .setLabel("加载中")
////                        .setDetailsLabel("Downloading data")
//                            .setCancellable(true)
//                            .setAnimationSpeed(2)
//                            .setDimAmount(0.5f);
//                    smsProgress.show();
//                    UserManager.requestSmsCode(activity, phoneNo, "reg", new ResponseWithFailListener<JSONObject>() {
//                        @Override
//                        public void onResponse(String tag, final JSONObject response) {
//                            //Log.d(TAG_LOG,response.toString());
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//
//                                    if (response.toString() != null) {
//                                        handler.sendEmptyMessage(MSG_COUNT);
//                                        Toast.makeText(activity, "短信已发送", Toast.LENGTH_LONG).show();
//                                    } else {
//                                        btn_register_code.setClickable(true);
//                                    }
//                                }
//                            });
//
//                        }
//
//                        @Override
//                        public void onError(final String tag, final Throwable t) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    Log.d(TAG_LOG, tag + ":" + t.getMessage());
//                                    Log.d(TAG_LOG, "短信发送失败");
//                                    smsProgress.dismiss();
//                                    Toast.makeText(activity, "短信发送失败，稍后继续请求", Toast.LENGTH_LONG).show();
//                                    btn_register_code.setClickable(true);
//                                }
//                            });
//                        }
//
//
//                        @Override
//                        public void onFail(String requestTag, final String response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    try {
//                                        Gson gson = new Gson();
//                                        ResultErrorVo resultErrorVo = gson.fromJson(response, ResultErrorVo.class);
//                                        btn_register_code.setClickable(true);
//                                        smsProgress.dismiss();
//                                        Toast.makeText(activity, "null".equals(resultErrorVo.getMsg()) ? "短信验证码发送失败" : resultErrorVo.getMsg(), Toast.LENGTH_LONG).show();
//                                    } catch (Exception e) {
//                                        e.printStackTrace();
//                                    }
//
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onFinish() {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    smsProgress.dismiss();
//                                }
//                            });
//                        }
//                    });
//                } else if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_QPOP) {
//                    if (canSendSms) {
//                        Toast.makeText(activity, "该号码已注册", Toast.LENGTH_LONG).show();
//                        return;
//                    }
//                    smsProgress = KProgressHUD.create(activity)
//                            .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
////                        .setLabel("加载中")
////                        .setDetailsLabel("Downloading data")
//                            .setCancellable(true)
//                            .setAnimationSpeed(2)
//                            .setDimAmount(0.5f);
//                    smsProgress.show();
//                    UserManager.requestIjkSmsCode(activity, phoneNo, new ResponseCallback<Integer>() {
//
//                        @Override
//                        public void onSuccess(String requestTag, final Integer response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    btn_register_code.setClickable(true);
//                                    smsProgress.dismiss();
//                                    if (response > 0) {
//                                        handler.sendEmptyMessage(MSG_COUNT);
//                                        Toast.makeText(activity, "短信已发送", Toast.LENGTH_LONG).show();
//                                    } else {
//                                        ToastUtil.show(activity, "短信发送失败，稍后继续请求");
//                                    }
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onFailure(String requestTag, String message) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    smsProgress.dismiss();
//                                    Toast.makeText(activity, "短信发送失败，稍后继续请求", Toast.LENGTH_LONG).show();
//                                    btn_register_code.setClickable(true);
//                                }
//                            });
//                        }
//                    });
//                }
//                break;
//            case R.id.login_wechat_lin:
//                SendAuth.Req req = new SendAuth.Req();
//                req.scope = "snsapi_userinfo";
//                req.state = "nfwx";
//                iwxapi.sendReq(req);
//                break;
//        }
//
//    }
//
//    public Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case MSG_COUNT:
//                    if (countDown >= 0) {
//                        btn_register_code.setText(countDown-- + "s");
//                        handler.sendEmptyMessageDelayed(MSG_COUNT, 1000);
//                    } else {
//                        btn_register_code.setClickable(true);
//                        countDown = 60;//复位，60s倒计时完后可以继续倒计时
//                        btn_register_code.setText("获取验证码");
//                    }
//                    break;
//            }
//
//        }
//    };
//
//
//    public Dialog getDialog(View view) {
//        Dialog dialog = new Dialog(activity, R.style.MyDialog);
//        dialog.setContentView(view);
//        Window dialogWindow = dialog.getWindow();
//        WindowManager m = activity.getWindowManager();
//        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
//        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight() * 0.8); // 高度设置为屏幕的0.6，根据实际情况调整
//        p.width = (int) (d.getWidth() * 0.6); // 宽度设置为屏幕的0.65，根据实际情况调整
//        dialogWindow.setAttributes(p);
//        return dialog;
//    }
//
//    @Override
//    public void onNetworkChange(int networkType) {
//        if (NetworkUtil.getNetworkType(activity) == NetworkUtil.WIFI_NETWORK) {
//            WifiHelper.getInstance()._testIfNeedLogon(new ValueCallback<Boolean>() {
//                @Override
//                public void onReceiveValue(Boolean value) {
//                    if (value) {
//                        shandler.sendEmptyMessage(0);
//                    } else {
//                        shandler.sendEmptyMessage(1);
//                    }
//                }
//            }, "wifi");
//        } else {
//            shandler.sendEmptyMessage(0);
//        }
//    }
//
//    public void onDestroy() {
//        NetworkHelper.getInstance().removeListener(this);
//    }
//
//    Handler shandler = new Handler() {
//        @Override
//        public void dispatchMessage(Message msg) {
//            super.dispatchMessage(msg);
//            switch (msg.what) {
//                case 0:
//
//                    login_wechat_lin.setVisibility(View.VISIBLE);
//
//                    break;
//                case 1:
//
//                    login_wechat_lin.setVisibility(View.INVISIBLE);
//
//                    break;
//            }
//
//        }
//    };
//}
